package com.guigu.buy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.guigu.buy.pojo.ProductType;
import com.guigu.buy.mapper.ProductTypeMapper;
import com.guigu.buy.service.IProductTypeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.guigu.buy.vo.ProductTypeVo;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author com.guigu
 * @since 2024-05-15
 */
@Service
public class ProductTypeServiceImpl extends ServiceImpl<ProductTypeMapper, ProductType> implements IProductTypeService {
    /**
     * 主页获取下拉框的值下拉框
     * @return
     */
    @Override
    public List<ProductType> getProductType(int parentId) {

        List<ProductType> productType = getSubProductType(parentId);

        for (ProductType type : productType) {
            type.setChildren(getProductType(type.getId())); // 递归查询子分类
        }
        return productType;
    }
    /**
     * 查询所有商品类型
     * @return
     */
    @Override
    public PageInfo<ProductType> selectAll(int pageNo, int pageSize, ProductTypeVo vo) {
        QueryWrapper<ProductType> wrapper=new QueryWrapper<>();
        wrapper.eq("delete_status","0");
        if(!StringUtils.isEmpty(vo.getProductTypeName())){
            wrapper.eq("product_type_name",vo.getProductTypeName());
        }if(!StringUtils.isEmpty(vo.getLevel()) && vo.getLevel()!="0"){
            wrapper.eq("level",vo.getLevel());
        }


        PageHelper.startPage(pageSize,pageNo);
        List<ProductType> list =this.list(wrapper);
        PageInfo<ProductType> pageInfo=new PageInfo<>(list);
        return pageInfo;
    }
    //删除商品类型
    @Override
    public Boolean delOne(int id) {
        ProductType productType=new ProductType();
        productType.setDeleteStatus("1");
        productType.setId(id);
        Boolean fale= this.updateById(productType);
        return fale;
    }


    /**
     * 主页获取下拉框的值下拉框
     * @return
     */
    public List<ProductType> getSubProductType(int parentId){
        QueryWrapper<ProductType> wrapper = new QueryWrapper<ProductType>();
        wrapper.eq("parent_Id",parentId);
        wrapper.eq("delete_status",0);
        return this.list(wrapper);
    }

}
